//
//  OANameListController.m
//  OASystem
//
//  Created by zhoupengfei on 15/4/21.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OANameListController.h"
#import "HttpTools.h"

#import "GDataDefines.h"
#import "MBProgressHUD.h"
#import "OANameListModel.h"
#import "OASubNameListModel.h"
#import "HeaderView.h"
#import "OANewEmailController.h"
//#import "OANewSMSViewController.h"
#import "MKTreeView.h"

@interface OANameListController ()

@property (nonatomic,strong)MKTreeView *mkView;


@end

@implementation OANameListController
-(NSMutableArray*)groupArray{

    if (_groupArray == nil) {
        _groupArray = [NSMutableArray array];
    }
    return _groupArray;
}

-(NSMutableArray*)rowArray{
    
    if (_rowArray == nil) {
        _rowArray = [NSMutableArray array];
    }
    return _rowArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [self loadData];
}

#pragma mark 取消
- (IBAction)cancle:(id)sender {
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 确定
- (IBAction)confir:(id)sender {
    if ([_string isEqualToString:@"huiqian"]){
//  NSString * selectPersons = [tempArray componentsJoinedByString:@","];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"segment":self.setpString},@{@"staff":_selectPersons},@{@"ryorder":@"0"},@{@"famxml":self.body}];
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetStaffListDeliVerMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
//        self.rightItem.enabled = NO;
        GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: [JSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
        GDataXMLElement *rootElement = [doc rootElement];
        NSArray *arrData= [rootElement elementsForName:@"info"];
        NSString * message= nil;
        for (GDataXMLElement * subXMLElement in arrData) {
            GDataXMLElement * element = [[subXMLElement elementsForName:@"msg"] objectAtIndex:0];
            message  = [element stringValue];
        }
    
            [self.view makeToast:@"发送成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
            [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
    
        
    } failure:^(NSError *error) {
//        self.rightItem.enabled = YES;
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.view makeToast:@"发送失败"];
    }];

}

else{
    NSMutableArray * tempArray = [NSMutableArray array];
    for (OANameListModel * nameLisModel in self.groupArray) {
        for (OASubNameListModel * subModel in nameLisModel.subArray) {
            if (subModel.isSelected) {
                [tempArray addObject:subModel];
                if ([self.delegate respondsToSelector:@selector(didSelecteName:)]) {
                    [self.delegate didSelecteName:tempArray];
                }
            }
        }
    }
//    [self dismissViewControllerAnimated:YES completion:nil];

    [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark 加载网络数据
-(void)loadData{
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId}];
    
    [HttpTools requestWithURL:KExchTree arrays:array method:KGlobalTree success:^(id JSON) {
        
        NSArray *arry = JSON[@"data"];
        self.dataSource = arry.firstObject;
        
        NSArray *dataArray = self.dataSource[@"Parent"];

        MKTreeView *view = [[MKTreeView instanceView] initTreeWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) dataArray:dataArray haveHiddenSelectBtn:NO haveHeadView:NO isEqualX:NO];
        self.mkView = view;
        
        [self.view addSubview:view];

    } failure:^(NSError *error) {
        
    }];
    
   
}


-(void)clickHeaderView{
    [self.tableView reloadData];
}-(void)clickHeaderView2{
    [self.tableView reloadData];
}



//    OANameListModel * nameListModel = self.groupArray[indexPath.section];
//    OASubNameListModel * subModel = nameListModel.subArray[indexPath.row];
//    subModel.isSelected = !subModel.isSelected;
//    [self.tableView reloadData];

-(void)TableViewCellDidSelectWithProvinceName:(NSString *)provinceName andCityName:(NSString *)cityName
{
    NSLog(@"provinceName--%@,cityName--%@",provinceName,cityName);
    
    NSString *message = [NSString stringWithFormat:@"%@,%@",provinceName,cityName];
    [[[UIAlertView alloc] initWithTitle:@"您选中了" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
}


@end
